<?php

include_once('activite.class.php');
include_once('myPDO.class.php');

class ActiviteDAO {
   // Creation Activité
    public function createActivite(/* Activite */ $activite, /* myPDO */ $db) {
        try {
            $stmt = $db->prepare("INSERT INTO Activite (nom, instructionActi, sonReussite, bordureType, estPublic, type, Adulte_identifiant) VALUES (:nom, :ins, :son, :bordure, :public, :type, :adulte)");
            $nom = utf8_decode($activite->getNomActi());
            $ins = utf8_decode($activite->getInstruction());
            $son = utf8_decode($activite->getSon());
            $public = utf8_decode($activite->getPublic());
            $bordure = utf8_decode($activite->getTypeBordure());
            $type = utf8_decode($activite->getType());
            $adulte= utf8_decode($activite->getAdulte());
            $stmt->bindParam(':nom', $nom);
            $stmt->bindParam(':ins', $ins);
            $stmt->bindParam(':son', $son);
            $stmt->bindParam(':bordure', $bordure);
            $stmt->bindParam(':public', $public);
            $stmt->bindParam(':type', $type);
            $stmt->bindParam(':adulte', $adulte);
            $stmt->execute();

            $a = $db->lastInsertId();
            return $a;
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
// mise à jour activité
    public function updateActivite(/* Adulte */ $activite, /* myPDO */ $db) {
        try {
            $stmt = $db->prepare("UPDATE Activite SET libelle=:lib, layout=:lay WHERE idActi=:id ;");
            $lib = utf8_decode($activite->getLibelle());
            $lay = utf8_decode($activite->getLayout());
            $id = $activite->getIdentifiant();
            $stmt->bindParam(':lib', $lib);
            $stmt->bindParam(':lay', $lay);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
    
    //Mise à jour du son
    public function updateSoundActiviteSound(/* Adulte */ $activite, /* myPDO */ $db) {
        try {
            $stmt = $db->prepare("UPDATE Activite SET sonReussite=:son WHERE idActi=:id ;");
            $son = utf8_decode($activite->getSon());
            $id = $activite->getIdentifiant();
            $stmt->bindParam(':son', $son);
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }
 // Recherche Activité avec le nom
    public function findActivite(/* Activite */ $activite, /* myPDO */ $db) {
        
        $stmt = $db->prepare("SELECT * FROM Activite WHERE nom = :nom ;");
        $nom = utf8_decode($activite->getNomActi());
        $stmt->bindParam(':nom', $nom);
        $stmt->execute();
        
        $row = $stmt->fetchObject();
       
        if ($row == null) {
            return null;
        } else {
            $acti = new Activite($row->idActi, utf8_encode($row->nom), utf8_encode($row->sonReussite),utf8_encode($row->instructionActi),utf8_encode($row->bordureType), utf8_encode($row->estPublic), utf8_encode($row->type));
            return $acti;
        }
    }
  // Recherche activité avec l'id 
    public function findActivite2(/* Activite */ $activite, /* myPDO */ $db) {
        
        $stmt = $db->prepare("SELECT * FROM Activite WHERE idActi = :id ;");
        $id = $activite->getIdentifiant();
        $stmt->bindParam(':id', $id);
        $stmt->execute();
        
        $row = $stmt->fetchObject();
       
        if ($row == null) {
            return null;
        } else {
            $acti = new Activite($row->idActi, utf8_encode($row->nom), utf8_encode($row->sonReussite),utf8_encode($row->instructionActi),utf8_encode($row->bordureType), utf8_encode($row->estPublic), utf8_encode($row->type));
            return $acti;
        }
    }
// rechercher tous les activités
    public function findAllActivites($act,/* myPDO */ $db) {
        $tabRes = array();
        $i = 0;
        $nom = $act->getNomActi();
        $t = $nom."%";
        $stmt = $db->prepare("SELECT * FROM Activite WHERE nom LIKE :nom;");
        $stmt->bindParam(':nom', $t);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $activite =new Activite($row->idActi, utf8_encode($row->nom), utf8_encode($row->sonReussite),utf8_encode($row->instructionActi),utf8_encode($row->bordureType), utf8_encode($row->estPublic), utf8_encode($row->type));
            $tabRes[$i] = $activite;
            $i++;
        }
        return $tabRes;
    }
 // Rechercher les activités appertenant  un adulte    
    public function findAllActivitesM ($adulte,$act,$db){
        $tabRes = array();
        $i = 0;
        $nom = $act->getNomActi();
        $t = $nom."%";
        $stmt = $db->prepare("SELECT * FROM Activite where Adulte_identifiant = :id AND nom LIKE :nom;");
        $id = $adulte->getIdentifiant();
        $stmt->bindParam(':nom', $t);
        $stmt->bindParam (':id',$id);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $activite =new Activite($row->idActi, utf8_encode($row->nom), utf8_encode($row->sonReussite),utf8_encode($row->instructionActi),utf8_encode($row->bordureType), utf8_encode($row->estPublic), utf8_encode($row->type));
            $tabRes[$i] = $activite;
            $i++;
        }
        return $tabRes;
    }
/*
    public function displayAllActivites( $db) {
        $stmt = $db->prepare("SELECT * FROM Activite ;");
        $res = $stmt->execute();
        echo "<table>";
        while ($row = $stmt->fetchObject()) {
            echo "<tr>";
            echo "<td>" . $row->identifiant . "</td>";
            echo "<td>" . $row->libelle . "</td>";
            echo "<td>" . $row->layout . "</td>";
            echo "</tr>";
        }
        echo "</table>";
    }
   */
    
  // Rechercher les activités publiques 
    
    public function findAllActivitesPublic ($act,$db) {
        $tabRes = array();
        $i = 0;
        $nom = $act->getNomActi();
        $t = $nom."%";
        $stmt = $db->prepare ("SELECT * FROM Activite where estPublic = 1 AND nom LIKE :nom;");
        $stmt->bindParam(':nom', $t);
        $res = $stmt->execute();
        while ($row = $stmt->fetchObject()) {
            $activite =new Activite($row->idActi, utf8_encode($row->nom), utf8_encode($row->sonReussite),utf8_encode($row->instructionActi),utf8_encode($row->bordureType), utf8_encode($row->estPublic), utf8_encode($row->type));
            $tabRes[$i] = $activite;
            $i++;
        }
        return $tabRes;
    }
    
  // Supprimer une activité

    public function deleteActivite(/* Eleve */ $activite, /* myPDO */ $db) {
        try {
            $stmt = $db->prepare("DELETE FROM Activite WHERE idActi = :id ;");
            $id = $activite->getIdentifiant();
            $stmt->bindParam(':id', $id);
            $stmt->execute();
        } catch (Exception $e) {
            echo 'Exception re�ue : ', $e->getMessage(), "\n";
        }
    }

}

?>